﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace Web.Views.Shared
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Membership.DeleteUser("agent");
            
            if(!Page.IsPostBack)
                FormsAuthentication.SignOut();
            string s = "http://mp3.salkin.cn/g_u_l_&_yar/dj/One%20Way.wma";// "url:https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8&body=%e8%ae%a2%e5%8d%95%e5%8f%b7%3a0904220008%2c%e8%a1%8c%e7%a8%8b%3a%e6%88%90%e9%83%bd-%e4%b8%87%e5%b7%9e&notify_url=http%3a%2f%2flocalhost%2feasydp%2fViews%2fAilPay%2fAliPayNotify.aspx&out_trade_no=0904220008&partner=2088002061866935&payment_type=1&return_url=http%3a%2f%2flocalhost%2feasydp%2fViews%2fAilPay%2fAliPayReceive.aspx&royalty_parameters=easydp%40yeah.net%5e5.00%5e%e7%81%ab%e8%bd%a6%e7%a5%a8%7cpanbbb%40hotmail.net%5e1.00%5e%e5%b9%b3%e5%8f%b0%e4%ba%a4%e6%98%93%e8%b4%b90904220008%7cguyy%40midea.com.cn%5e0.06%5e%e6%94%af%e4%bb%98%e8%b4%b9%e7%94%a80904220008&royalty_type=10&seller_email=easydp%40yeah.net&service=create_direct_pay_by_user&show_url=http%3a%2f%2fwww.easydp.net&subject=%e7%81%ab%e8%bd%a6%e7%a5%a8&total_fee=6.00&sign=aa282ccd643168b96b85a59a2a9b9476&sign_type=MD5";
            //url:https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8&body=订单号:0904220008,行程:成都-万州&notify_url=http://localhost/easydp/Views/AilPay/AliPayNotify.aspx&out_trade_no=0904220008&partner=2088002061866935&payment_type=1&return_url=http://localhost/easydp/Views/AilPay/AliPayReceive.aspx&royalty_parameters=easydp@yeah.net^5.00^火车票|panbbb@hotmail.net^1.00^平台交易费0904220008|guyy@midea.com.cn^0.06^支付费用0904220008&royalty_type=10&seller_email=easydp@yeah.net&service=create_direct_pay_by_user&show_url=http://www.easydp.net&subject=火车票&total_fee=6.00&sign=aa282ccd643168b96b85a59a2a9b9476&sign_type=MD5
            s = HttpUtility.UrlDecode(s);
            
        }

        protected void ImgbtnLogin_Click(object sender, ImageClickEventArgs e)
        {
            
            string userName = txtUserName.Value.Trim();
            string password = txtPassword.Value.Trim();
            //如果用户名和密码为空 则返回 不做任何处理
            if (userName == "" || password == "")
            {
                txtMsg.InnerText = "请输入完整的用户名和密码.";
                return;
            }
            if (Membership.ValidateUser(userName, password))
            {
                MembershipUser userObj = Membership.GetUser(Context.User.Identity.Name);
                string[] roles = Roles.GetRolesForUser(userName);
                string userRoles="";
                foreach (string item in roles)
                {
                    userRoles += item+",";                   
                }
                FormsAuthenticationTicket Ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(120), false, userRoles.TrimEnd(new char[]{','}), "/");
                string HashTicket = FormsAuthentication.Encrypt(Ticket);
                HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, HashTicket);
                Response.Cookies.Add(UserCookie);
                if (Context.Request["ReturnUrl"] != null)
                {
                    if (userRoles.Contains("Agents"))
                    {
                        //初始化代理商信息
                        (new Easydp.Controllers.AgentController()).InitAgentInfo(userName);
                    }
                    Response.Redirect(Context.Request["ReturnUrl"]);
                }
                else
                {
                    string url="~/Views/Common/default.aspx";
                    if (userRoles.Contains("Admins"))
                    {
                        url = "~/Views/Admin/ManagerUser.aspx";
                    }
                    else if (userRoles.Contains("Agents"))
                    {
                        //初始化代理商信息
                        //(new Easydp.Controllers.AgentController()).InitAgentInfo(userName);
                        url = "~/Views/AgentManager/Default.aspx";
                    }
                    Response.Redirect(url);
                }
            }
            else
            {
                txtMsg.InnerText = "用户名和密码不匹配.请重新输入.";
            }
        }        
    }
}
